package com.arvato.wxwork.controller.admin.store;

import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.arvato.common.ftp.FtpTool;
import com.arvato.minipro.bean.bean_dto.common.PageData;
import com.arvato.minipro.bean.bean_dto.common.Query;
import com.arvato.minipro.bean.bean_dto.common.Result;
import com.arvato.wxwork.bean.bean_do.store.WxworkStoreDo;
import com.arvato.wxwork.bean.bean_vo.store.WxworkStoreVo;
import com.arvato.wxwork.service.store.WxworkStoreService;


/**
 * 企业微信门店管理Controller
 * @author zhan655
 *
 */
@Controller
@RequestMapping("/admin/wxwork/store/")
public class WxworkStoreController {
	
	private Logger logger = LoggerFactory.getLogger(WxworkStoreController.class);

	@Autowired
	private WxworkStoreService wxworkStoreService;
	
	@Autowired
	private FtpTool ftpTool;
	/**
	 * 获取门店列表
	 * @param req
	 * @param param
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="/list")
	public PageData liststore(HttpServletRequest req, @RequestParam Map<String, Object> param) {
		Query query = new Query(param);
		logger.info("门店列表");
		PageData pageData = wxworkStoreService.listStoreByQuery(query, query.getPageNum(), query.getPageSize());
		return pageData;
	}
	
	/**
	 * 获取一个门店的详情
	 * @param req
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="/detail")
	public Result storeDetail(String storeId) {
		WxworkStoreVo storeInfoVo = wxworkStoreService.getStoreById(storeId);
		return Result.success(storeInfoVo);
	}
	
	/**
	 * 新增门店
	 * @param wxworkstoreInfoDo
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/insert")
	public Result insert(WxworkStoreDo storeDo,  @RequestParam("file") MultipartFile file){
		// 新增之前 先检查门店编号是否重复
		boolean flag = wxworkStoreService.isExist(storeDo.getStoreCode());
		if(flag) {
			return Result.fail("该编号门店已存在。");
		}
		String fileName = ftpTool.uploadFile(file);
		System.out.println("文件名：" + fileName);
		if(fileName == null) {
			logger.info("图片上传失败");
			return Result.fail("图片上传失败");
		}
		storeDo.setFileName(fileName);
		try {
			int row = wxworkStoreService.insert(storeDo);
			if(row == 1) {
				return Result.success("新增成功。");
			}
		} catch (Exception e) {
			e.printStackTrace();
			return Result.fail("新增失败" + e.getMessage());
		}
		return Result.fail("新增失败");
	}
	
	/**
	 * 新增门店不带图片
	 * @param wxworkstoreInfoDo
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/insert/nofile")
	public Result insertNoFile(WxworkStoreDo storeDo){
		// 新增之前 先检查门店编号是否重复
		boolean flag = wxworkStoreService.isExist(storeDo.getStoreCode());
		if(flag) {
			return Result.fail("该编号门店已存在。");
		}
		try {
			int row = wxworkStoreService.insert(storeDo);
			if(row == 1) {
				return Result.success("新增成功。");
			}
		} catch (Exception e) {
			e.printStackTrace();
			return Result.fail("新增失败" + e.getMessage());
		}
		return Result.fail("新增失败");
	}
	
	
	
	/**
	 * 更新门店
	 * @param wxworkstoreInfoDo
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/update")
	public Result update(WxworkStoreDo wxworkstoreDo, @RequestParam("file") MultipartFile file){
		String fileName = ftpTool.uploadFile(file);
		System.out.println("文件名：" + fileName);
		if(fileName == null) {
			logger.info("图片上传失败");
			return Result.fail("图片上传失败");
		}
		wxworkstoreDo.setFileName(fileName);
		try {
			int row = wxworkStoreService.update(wxworkstoreDo);
			if(row == 1) {
				return Result.success("修改成功");
			}
		} catch (Exception e) {
			e.printStackTrace();
			return Result.fail("修改失败" + e.getMessage());
		}
		return Result.fail("修改失败");
	}
	
	/**
	 * 删除门店
	 * @param storeId
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/delete")
	public Result delete(String storeId){
		try {
			int row = wxworkStoreService.delete(storeId);
			if(row == 1) {
				return Result.success("删除成功。");
			}
		} catch (Exception e) {
			e.printStackTrace();
			return Result.fail("删除失败。" + e.getMessage());
		}
		return Result.fail("删除失败");
	}
	
}

